library(fixest)
library(tidyverse)

# Definitions
setFixest_dict(
    c(
        GRUENE_share = "\\makecell{Percentage of votes\\\\ for Green Party}",
        Verbandsgemeindeschluessel = "Municipality",
        Kreisschluessel = "County",
        Land = "State",
        year = "Year",
        GRUENE_share_diff = "\\makecell{$\\Delta$Percentage of votes\\\\ for Green Party}",
        schaefer_low_p = "Flooding: weakly affected (county)",
        schaefer_high_p = "Flooding: highly affected (county)",
        ratio_total_p = "Share of buildings affected (municipality)",
        ratio_total_p_county = "Share of buildings affected (county)",
        ratio_total_p_0TRUE = "Share of buildings affected > 0% (municipality)"
    )
)
setFixest_etable(
    fitstat = c("my", "n", "ar2", "wr2"),
    style.tex = style.tex("aer"),
    digits.stats = 4
)


# Data
clean <- function(ff) {
    read_csv(ff) %>%
        group_by(Verbandsgemeindeschluessel) %>%
        mutate(
            GRUENE_share_diff =
                GRUENE_share - dplyr::lag(GRUENE_share, order_by = year)
        ) %>%
        ungroup() %>%
        mutate(one = 1) %>%
        group_by(Kreisschluessel, year, Land, schaefer_low_p, schaefer_high_p) %>%
        mutate(
            ratio_total_p_county = weighted.mean(ratio_total_p, pop),
        ) %>%
        ungroup() %>%
        mutate(ratio_total_p_0 = ratio_total_p > 0)
}

df <- clean("../data/regdata.csv")

# Regressions

r1 <- feols(
    GRUENE_share ~ schaefer_low_p + schaefer_high_p | Verbandsgemeindeschluessel + year,
    cluster = ~Kreisschluessel,
    data = df,
    weights = ~one,
    subset = ~ year >= 2017
)

r2 <- feols(
    GRUENE_share ~ ratio_total_p_county | Verbandsgemeindeschluessel + year,
    cluster = ~Kreisschluessel,
    data = df,
    weights = ~one,
    subset = ~ year >= 2017
)

r3 <- feols(
    GRUENE_share ~ ratio_total_p | Verbandsgemeindeschluessel + year,
    cluster = ~Kreisschluessel,
    data = df,
    weights = ~one,
    subset = ~ year >= 2017
)

r4 <- feols(
    GRUENE_share ~ ratio_total_p | Verbandsgemeindeschluessel + Kreisschluessel^year,
    cluster = ~Kreisschluessel,
    data = df,
    weights = ~Gueltige,
    subset = ~ year >= 2017
)

r5 <- feols(
    GRUENE_share ~ ratio_total_p_0 | Verbandsgemeindeschluessel + Kreisschluessel^year,
    cluster = ~Kreisschluessel,
    data = df,
    weights = ~Gueltige,
    subset = ~ year >= 2017
)

r6 <- feols(
    GRUENE_share ~ ratio_total_p + ratio_total_p_0 | Verbandsgemeindeschluessel + Kreisschluessel^year,
    cluster = ~Kreisschluessel,
    data = df,
    weights = ~Gueltige,
    subset = ~ year >= 2017
)

# Tables
results <- list(r1, r2, r3, r4, r5, r6)
esttex(
    results,
    file = "../results/tables/table_b1.tex",
    replace = TRUE,
    digits = 3,
    drop = "Constant",
    extralines = list(
        "-_Weights" = c("", "", "", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$")
    )
)
